<template>
  <div>
    <div
      style="background: #fff; padding: 20px;height: 90vh;margin-top: 80px;overflow: hidden;overflow-y: scroll;"
    >
      <div style="width: 80%;margin-left: 10%;">
        <h4>必游景点TOP5</h4>
        <div
          class="each-fun flex-between cp"
          v-for="(item, index) in funListTop5"
          @click="toDetail(item)"
          :key="index"
        >
          <div class="l">
            <div class="flex-between">
              <div class="flex-center">
                <div class="no">{{ index + 1 }}</div>
                <div class="name">{{ item.funName }}</div>
              </div>
              <div class="comments">{{ item.comments.length }}条评论</div>
            </div>
            <div style="margin-top:20px;">
              <el-rate
                v-model="item.rate"
                disabled
                show-score
                text-color="#ff9900"
                :score-template="item.rate + ''">
              </el-rate>
            </div>
            <div class="desc">
              {{ item.funDesc }}
            </div>
          </div>

          <div class="r flex-between">
            <img class="img1" :src="item.pics[0]" alt="" />
            <div style="width: 50%;">
              <img v-if="item.pics.length>1" class="img2" :src="item.pics[1]" alt="" />
              <img v-if="item.pics.length>2" class="img3" :src="item.pics[2]" alt="" />
            </div>
          </div>
        </div>

        <h4>全部景点</h4>
        <div class="flex-center">
          <div>景点类型：</div>
          <el-radio-group style="margin-top: 10px;" v-model="type" @change="changeType">
            <el-radio label="all">全部</el-radio>
            <el-radio label="运动保健型">运动保健型</el-radio>
            <el-radio label="游乐刺激型">游乐刺激型</el-radio>
            <el-radio label="观赏体验型">观赏体验型</el-radio>
            <el-radio label="文化休闲型">文化休闲型</el-radio>
            <el-radio label="室内消遣型">室内消遣型</el-radio>
          </el-radio-group>
        </div>
        <div class="flex-center">
          <div>票价范围：</div>
          <el-radio-group style="margin-top: 10px;" v-model="priceRange" @change="changeType">
            <el-radio label="all">全部</el-radio>
            <el-radio label="1">100以内</el-radio>
            <el-radio label="2">100~200</el-radio>
            <el-radio label="3">200以上</el-radio>
          </el-radio-group>
        </div>
        
        <div class="imgs">
          <div
            v-for="(item, index) in funListFilter"
            :key="index"
            class="each"
            @click="toDetail(item)"
          >
            <div style="width:100%;overflow: hidden;">
              <img :src="item.pics[0]" alt="" />
            </div>
            
            <div class="name-bottom">{{ item.funName }}</div>
          </div>
        </div>
        <div style="text-align: center;">
          <el-pagination
            @current-change="handleCurrentChange"
            small
            layout="prev, pager, next"
            :total="total"
          >
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getFun } from "../service";
export default {
  data() {
    return {
      userInfo: {},
      funList: [],
      funListFilter: [],
      total: 0,
      funListTop5: [],
      pageSize: 10,
      pageNum: 1,
      type: 'all',
      priceRange: 'all'
    };
  },
  created() {
    this.getFun();
  },
  methods: {
    changeType() {
      this.pageNum = 1;
      this.handlePage()
    },
    getFun() {
      getFun().then((res) => {
        let data = []
        for (let item of res.data) {
          const comments = item.comments
          item = item.funInfo
          item.pics = item.pics.split(",");
          item.comments = comments
          item.tickets = item.tickets
          item.rate = 0
          let sum = 0;
          let score = 0
          for (let c of item.comments) {
            sum++
            score += (c.mjfw + c.ywty) / 2
          }
          if (sum) {
            item.rate = Number((score / sum).toFixed(1))
          }
          data.push(item)
        }
        this.funList = data;
        this.funListTop5 = this.funList
          .sort((a, b) => {
            return b.rate - a.rate;
          })
          .slice(0, 5);
        console.log(this.funListTop5)
        this.handlePage();
      });
    },
    handlePage() {
      let funList = JSON.parse(JSON.stringify(this.funList))
      if (this.type !== 'all') {
        funList = funList.filter(v => v.types.includes(this.type))
      }
      if (this.priceRange !== 'all') {
        switch (this.priceRange) {
          case '1':
            funList = funList.filter(v => Number(v.price) <= 100)
            break;
          case '2':
            funList = funList.filter(v => Number(v.price) >= 100 && Number(v.price) <= 200)
            break;
          case '3':
            funList = funList.filter(v => Number(v.price) > 200)
            break;
        }
      }
      this.total = funList.length
      this.funListFilter = funList.slice(
        (this.pageNum - 1) * this.pageSize,
        this.pageSize * this.pageNum
      );
    },
    handleCurrentChange(index) {
      this.pageNum = index;
      this.handlePage();
    },
    toDetail(params) {
      this.$router.push({
        name: "Detail",
        params: {
          id: params._id,
        },
      });
      localStorage.setItem(
        "meiyaFunDetail",
        JSON.stringify({
          id: params._id,
        })
      );
    },
  },
};
</script>

<style lang="scss" scoped>

.name {
  font-size: 20px;
  font-weight: bold;
  line-height: 40px;
}
.desc {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  /* autoprefixer: off */
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 7;
  word-break: break-all;
  line-height: 24px;
}

.each-fun {
  padding: 20px;
  margin-top: 10px;
  display: flex;

  .l {
    width: 48%;
    .no {
      float: left;
      margin-right: 18px;
      width: 20px;
      height: 20px;
      border-radius: 2px;
      background-color: #ff9d00;
      text-align: center;
      color: #fff;
      font-size: 14px;
    }
    .name {
      color: #ff9d00;
      font-size: 18px;
      line-height: 20px;
      font-weight: normal;
    }
    .comments {
      font-size: 12px;
      color: #ff9d00;
    }

    .desc {
      font-size: 16px;
      margin-top: 20px;
    }
  }

  .r {
    width: 48%;
    .img1 {
      width: 50%;
      height: 200px;
      margin-right: 10px;
    }
    .img2 {
      width: 100%;
      margin-bottom: 10px;
      height: 95px;
    }

    .img3 {
      width: 100%;
      height: 95px;
    }
  }
}

.flex-between {
  display: flex;
  justify-content: space-between;
}
.flex-center {
  display: flex;
  align-items: center;
}

.imgs {
  width: 100%;
  display: flex;
  flex-wrap: wrap;

  .each:hover {
    img {
      transform: scale(1.1)
    }
  }

  & > div {
    width: 20%;
    padding: 12px;

    img {
      width: 100%;
      height: 150px;
      cursor: pointer;
      transition: all 0.3s
    }

    .name-bottom {
      margin-top: 10px;
      width: 100%;
      text-align: center;
      height: 43px;
      line-height: 42px;
      padding: 0 10px;
    }
  }
  & > div:hover {
    color: #ff9d00;
  }
}

.cp {
  cursor: pointer;
}
</style>
